package com.engine.core.util;

import com.engine.core.interceptor.AbstractCommand;
import com.engine.core.interceptor.Command;
import com.engine.system.User;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class QueryUtil {
     public final static String ACTION="action";
     public final static String QUERY_TABLE="table";
     public final static String QUERY_RESOURCE="resId";
     public final static String QUERY_METHOD="method";

     /**
      * 是否将参数转成map形式
      */
     public final static String QUERY_PARAMS_MAP="isMapParam";

     /**
      * 是否转化参数
      */
     public final static String QUERY_PARAMS_ISCONVERT="isConvert";

     public final static String QUERY_PARAMS_URLENCODE="isUrlEncode";

     /**
      * 主键请求参数
      */
     public final static String QUERY_ID="id";

     public final static String QUERY_ASSOCIATION_ID="associationId";

     public final static String QUERY_ASSOCIATION="association";

     /**
      * body请求参数
      */
     public final static String QUERY_CONTENT="content";

     public final static String SAVE_MAINDETAIL_JOINS="joins";

     public final static String SAVE_MAINDETAIL_MAIN="joins";
     public final static String SAVE_MAINDEtAIL_JOINS_COPYTO="copyTo";

     public final static String QUERY_COLUMNS="columns";

     /**
      * 查询from部分
      */
     public final static String QUERY_FROM="from";
     /**
      * 查询where部分
      */
     public final static String QUERY_WHERE="where";

     public final static String QUERY_WHERE_GROUP="iswhereGroup";
     public final static String QUERY_ORDERBY="orderBy";
     public final static String QUERY_GROUPBY="groupBy";

     public final static String QUERY_PARAMS="params";

     public final static String QUERY_VALUES="values";

     public final static String QUERY_SQL_REPLACE="sqlReplace";

     //分页当前页
     public final static String PAGED_CURRENT="current";

     public final static String PAGED_OFFSET="offset";
     public final static String PAGED_LIMIT="limit";

     //分页每页数量
     public final static String PAGED_PAGE_SIZE="pageSize";

     public final static String PAGED_PAGE_TOTAL="total";


     /**
      * 0 只获取sql,不做结果查询
      * 1 需要输出结果
      */
     public final static String QUERY_FETHCHRESULT="fethchResult";

     public final static String QUERY_HAVING="having";

     public final static String QUERY_RESULT="data";

     public final static String QUERY_RESULT_SQL="sql";
     public final static String QUERY_RESULT_COLUMN_TYPE="columnType";

     public final static String QUERY_RESULT_PARAMS="params";

     /**
      *  该参数只适合cmd之间的相互传参
      *  强制从param里面获取所有的参数
      */
     public final static String GET_QUERY_PARAMS_FORCE="queryParamsForce";


     public final static String QUERY_METHOD_GET="get";


     public final static String QUERY_METHOD_POST="post";
     public final static String UPDATA_DATA="data";
     public final static String UPDATA_OTHERS="others";


     public final static String UPDATA_IS_BATCH="isbatch";

     public static class ResponseCode{
           public static final int SUCCESS=200;
           public static final int ERROR=500;

     }

     public static class ResponseFields{

     }




     public static Map<String, Object> request2Map(HttpServletRequest request) {
          // 参数Map
          Map properties = request.getParameterMap();
          // 返回值Map
          Map<String, Object> returnMap = new HashMap<String, Object>();
          Iterator<Map.Entry> entries = properties.entrySet().iterator();
          Map.Entry entry;
          String name = "";
          Object value = null;
          while (entries.hasNext()) {
               entry = (Map.Entry) entries.next();
               name = (String) entry.getKey();
                    Object valueObj = entry.getValue();
                    if (null == valueObj) {
                         value = null;
                    } else if (valueObj instanceof String[]) {
                         String[] values = (String[]) valueObj;
                         if (values.length == 1) {
                              value = values[0];
                         } else {
                              value = values;
                         }
                    } else {
                         value = valueObj.toString();
                    }
                    returnMap.put(name, value);
          }

          //获取request的输入流，并设置格式为UTF-8
          //将输入流数据放入StringBuilder
          StringBuilder result = new StringBuilder();
          try {
               BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
               String inputStr = null;
               while ((inputStr = streamReader.readLine()) != null) {
                    result.append(inputStr);
               }
          } catch (IOException e) {
          }
          returnMap.put("content",result.toString());
          //放入ip
          return returnMap;
     }



     public static AbstractCommand getCommand(Class<? extends Command> commandCls,User user, Map<String, Object> params){
               try {
                    AbstractCommand abstractCommand=(AbstractCommand)commandCls.getConstructor(User.class,Map.class).newInstance(user,params);
                    return abstractCommand;
               } catch (Exception e) {
                    e.printStackTrace();
               }
               return null;

     }


}
